package demo.xlj.yuyi.studentapp.shetuan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.feezu.liuli.timeselector.TimeSelector;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demo.xlj.yuyi.studentapp.R;
import demo.xlj.yuyi.studentapp.base.BaseRxActivity;
import demo.xlj.yuyi.studentapp.bean.AddHuoDongBean;
import demo.xlj.yuyi.studentapp.retrofit.RetrofitClient;
import demo.xlj.yuyi.studentapp.rxjava.RxSchedulers;
import demo.xlj.yuyi.studentapp.utils.SharePerferencesUtils;
import demo.xlj.yuyi.studentapp.utils.ToastUtil;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import top.wefor.circularanim.CircularAnim;

/**
 * 作者：项路俊 on 2018/3/2 0002
 * 邮箱：ThanksForCode@gmail.com
 * 介绍：社团成员申请活动
 */

public class ApplyHuoDongActivity extends BaseRxActivity {


    @BindView(R.id.ImageView_back)
    ImageView mImageViewBack;
    @BindView(R.id.TextView_Title)
    TextView mTextViewTitle;
    @BindView(R.id.EditText_Apply_Huo_Dong_Content)
    EditText mEditTextApplyHuoDongContent;
    @BindView(R.id.EditText_Apply_Huo_Dong_Who)
    EditText mEditTextApplyHuoDongWho;
    @BindView(R.id.EditTExt_Apply_Huo_Dong_Address)
    EditText mEditTExtApplyHuoDongAddress;
    @BindView(R.id.TextView_Apply_Huo_Dong)
    TextView mTextViewApplyHuoDong;
    @BindView(R.id.progressBar_Apply_Huo_Dong)
    ProgressBar mProgressBarApplyHuoDong;
    @BindView(R.id.button_Apply_Huo_Dong)
    Button mButtonApplyHuoDong;


    private String apply_time = "";
    private String content; //主题
    private String addre; //地址
    private String undertask; //承办方
    private String userid;


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {

                case 10:
                    mProgressBarApplyHuoDong.setVisibility(View.VISIBLE);
                    addRxDestroy(RetrofitClient.getInstance(ApplyHuoDongActivity.this)
                            .getApiService()
                            .addHuoDong(content, addre, undertask, apply_time, userid)
                            .compose(RxSchedulers.<AddHuoDongBean>io_main())
                            .subscribe(new Consumer<AddHuoDongBean>() {
                                @Override
                                public void accept(@NonNull AddHuoDongBean addHuoDongBean) throws Exception {

                                    if (addHuoDongBean.getStatus() == 10022) {
                                        ToastUtil.success(ApplyHuoDongActivity.this, "申请活动成功，具体进度请去我的社团里面查看", false, true);
                                    } else {
                                        ToastUtil.error(ApplyHuoDongActivity.this, "申请活动失败：" + addHuoDongBean.getInfo().getError(), true, true);
                                    }
                                    mHandler.sendEmptyMessage(11);
                                }
                            }, new Consumer<Throwable>() {
                                @Override
                                public void accept(@NonNull Throwable throwable) throws Exception {
                                    ToastUtil.error(ApplyHuoDongActivity.this, "申请活动失败：", true, true);
                                    mHandler.sendEmptyMessage(11);
                                }
                            }));

                    mEditTextApplyHuoDongContent.setText("");
                    mEditTExtApplyHuoDongAddress.setText("");
                    mEditTextApplyHuoDongWho.setText("");
                    mTextViewApplyHuoDong.setText("请选择申请活动的时间");
                    break;

                case 11:
                    mProgressBarApplyHuoDong.setVisibility(View.INVISIBLE);
                    CircularAnim.show(mButtonApplyHuoDong).go();

                    break;
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_huo_dong);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        if (intent == null){

        }else {
            mEditTextApplyHuoDongContent.setText(intent.getStringExtra("content"));
            mEditTextApplyHuoDongWho.setText(intent.getStringExtra("who"));
            mEditTExtApplyHuoDongAddress.setText(intent.getStringExtra("address"));
            mTextViewApplyHuoDong.setText(intent.getStringExtra("time"));
            apply_time = intent.getStringExtra("time");
        }
        userid = (String) SharePerferencesUtils.get(ApplyHuoDongActivity.this, "name", "test", 0);
        mTextViewTitle.setText("活动申请");
    }


    /**
     * 点击申请活动
     *
     * @param view
     */
    public void btn_apply_huo_dong(View view) {
        content = mEditTextApplyHuoDongContent.getText().toString();
        addre = mEditTExtApplyHuoDongAddress.getText().toString();
        undertask = mEditTextApplyHuoDongWho.getText().toString();

        if (TextUtils.isEmpty(content)) {
            mEditTextApplyHuoDongContent.setError("活动主题不能为空");
            return;
        }

        if (TextUtils.isEmpty(addre)) {
            mEditTExtApplyHuoDongAddress.setError("活动地址不能为空");
            return;
        }

        if (TextUtils.isEmpty(undertask)) {
            mEditTextApplyHuoDongWho.setError("活动申请人不能为空");
            return;
        }

        if (apply_time.equals("")) {
            ToastUtil.info(ApplyHuoDongActivity.this, "请输入活动时间", true, true);
            return;
        }

        if ("test".equals(userid)) {
            ToastUtil.info(ApplyHuoDongActivity.this, "用户身份检测失败，请退出重新登录", true, true);
            return;
        }

        //开启动画
        CircularAnim.hide(mButtonApplyHuoDong).go();


        mHandler.sendEmptyMessageDelayed(10, 618);


    }


    @OnClick({R.id.ImageView_back, R.id.TextView_Apply_Huo_Dong})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ImageView_back:
                ApplyHuoDongActivity.this.finish();
                break;
            case R.id.TextView_Apply_Huo_Dong:
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
                String format1 = format.format(date);
                TimeSelector timeSelector = new TimeSelector(ApplyHuoDongActivity.this, new TimeSelector.ResultHandler() {
                    @Override
                    public void handle(String time) {
                        apply_time = time;
                        mTextViewApplyHuoDong.setText(apply_time);
                    }
                }, format1, "2018-6-1 24:00");
                timeSelector.show();
                break;
        }
    }
}
